import { defineConfig } from 'vite'
import vue from '@vitejs/plugin-vue'
import { resolve } from 'path'
import AutoImport from 'unplugin-auto-import/vite'
import Components from 'unplugin-vue-components/vite'
import { ElementPlusResolver } from 'unplugin-vue-components/resolvers'

// https://vitejs.dev/config/
export default defineConfig({
  define: {
    global: {},
  },
  base: '/scb/',
  plugins: [
    // 自动导入element-plus组件和依赖
    AutoImport({
      imports: ['vue', 'vue-router'],
      resolvers: [ElementPlusResolver()],
    }),
    Components({
      resolvers: [ElementPlusResolver()],
    }),
    vue(),
  ],
  // 路径别名
  resolve: {
    alias: {
      '@': resolve(__dirname, 'src'),
    },
  },
  // 服务地址、端口配置
  server: {
    host: '0.0.0.0',
    port: 3000,
    open: true,
    // 本地代理服务
    proxy: {
      '/apiFets/': {
        // target: 'http://39.100.65.231:10091/yysvrapi/equ_ovr',
        // target: 'http://dev.yy-sport.com.cn:31019/ter/',
        // target: 'http://iot.zn-power.com:30450/yysvrapi/equ_ovr',
        target: 'http://10.42.0.110:10091/yysvrapi/equ_ovr',
        changeOrigin: true,
        rewrite: (path) => path.replace(/^\/apiFets/, ''),
      },
      '/record/': {
        // target: 'https://iot.zn-power.com/',
        target: 'http://192.168.8.104:8787/',
        changeOrigin: true,
        rewrite: (path) => path.replace(/^\/record/, ''),
      },
      // '/socket.io': {
      //   target: 'ws://localhost:3000',
      //   ws: true
      // }
    },
  },
  css: {
    preprocessorOptions: {
      // 全局引入公共样式
      scss: {
        // 引入多个公共样式
        additionalData: `@import "@/assets/scss/common";@import "@/assets/scss/element";`,
      },
    },
  },
  build: {
    outDir: 'scb',
    sourcemap: false, // 不允许debugger
    terserOptions: {
      compress: {
        //生产环境时移除console
        drop_console: true,
        drop_debugger: true,
      },
    },
    output: {
      // 超大静态资源拆分
      manualChunks(id) {
        if (id.includes('node_modules')) {
          return id.toString().split('node_modules/')[1].split('/')[0].toString()
        }
      },
      chunkFileNames: 'assets/js/[name]-[hash].js',
      entryFileNames: 'assets/js/[name]-[hash].js',
    },
    rollupOptions: {
      // external: ['element-plus'],
    },
    assetsInlineLimit: 4096, // 图片转 base64 编码的阈值
  },
})
